/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DBAddJFrame.java
 *
 * Created on Aug 12, 2011, 3:34:00 PM
 */
package org.opmviewer.jframes;

import java.awt.CardLayout;
import java.awt.Dialog;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.opmviewer.valueproviders.ValueProvider;

/**
 *
 * @author grzegorz
 */
public class DBAddJFrame extends JDialog {

    JanusValuesJPanel janusPanel;
    ValuesPanel currPanel = null;
    boolean confirmed = false;

    /** Creates new form DBAddJFrame */
    public DBAddJFrame(Dialog parent) {
        super(parent, true);
        initComponents();
        janusPanel = new JanusValuesJPanel();
        janusPanel.setActive(false);
        valuesPanel.add("Janus", janusPanel);
        //valuesPanel = janusPanel;
        pack();
        setLocationRelativeTo(parent);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    public String getDBName() {
        return dbNameField.getText();
    }

    public String getDBLocation() {
        return dbLocField.getText();
    }

    public String getOPMFileName() {
        return opmFileField.getText();
    }

    public boolean confirmed() {
        return confirmed;
    }

    public ValueProvider getValueProvider() {
        return importValuesCheckBox.isSelected()
                ? currPanel.getValueProvider() : null;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        dbNameField = new javax.swing.JTextField();
        dbNameLabel = new javax.swing.JLabel();
        dbLocLabel = new javax.swing.JLabel();
        dbLocField = new javax.swing.JTextField();
        browseButton1 = new javax.swing.JButton();
        confirmButton = new javax.swing.JButton();
        opmFileLabel = new javax.swing.JLabel();
        opmFileField = new javax.swing.JTextField();
        browseButton2 = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        importValuesCheckBox = new javax.swing.JCheckBox();
        valuesSourceComboBox = new javax.swing.JComboBox();
        comboBoxLabel = new javax.swing.JLabel();
        valuesPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        dbNameField.setText("Name");

        dbNameLabel.setText("Database name:");

        dbLocLabel.setText("Database location:");

        browseButton1.setText("Browse ...");
        browseButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButton1ActionPerformed(evt);
            }
        });

        confirmButton.setText("Confirm");
        confirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmButtonActionPerformed(evt);
            }
        });

        opmFileLabel.setText("OPM file to use:");

        browseButton2.setText("Browse ...");
        browseButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                browseButton2ActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        importValuesCheckBox.setText("Import artifact values into database");
        importValuesCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                importValuesCheckBoxActionPerformed(evt);
            }
        });

        valuesSourceComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Taverna Janus file" }));
        valuesSourceComboBox.setEnabled(false);
        valuesSourceComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                valuesSourceComboBoxActionPerformed(evt);
            }
        });

        comboBoxLabel.setText("Import from:");

        valuesPanel.setBackground(new java.awt.Color(226, 240, 235));
        valuesPanel.setEnabled(false);
        valuesPanel.setLayout(new java.awt.CardLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(opmFileLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(browseButton1)
                            .addComponent(dbLocField, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(browseButton2)
                            .addComponent(opmFileField, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE)))
                    .addComponent(dbLocLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(dbNameField, javax.swing.GroupLayout.DEFAULT_SIZE, 335, Short.MAX_VALUE))
                    .addComponent(dbNameLabel))
                .addGap(57, 57, 57)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(confirmButton, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(32, 32, 32)
                        .addComponent(cancelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(valuesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                    .addComponent(importValuesCheckBox, javax.swing.GroupLayout.DEFAULT_SIZE, 436, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(comboBoxLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(valuesSourceComboBox, 0, 325, Short.MAX_VALUE)))
                .addGap(24, 24, 24))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(dbNameLabel)
                                .addGap(12, 12, 12)
                                .addComponent(dbNameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(dbLocLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(dbLocField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(browseButton1)
                                .addGap(18, 18, 18)
                                .addComponent(opmFileLabel)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(opmFileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(browseButton2))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(importValuesCheckBox)
                                .addGap(29, 29, 29)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(comboBoxLabel)
                                    .addComponent(valuesSourceComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(26, 26, 26)
                                .addComponent(valuesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)))
                        .addGap(47, 47, 47)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(confirmButton)
                            .addComponent(cancelButton)))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE))
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean checkDBName() {
        if (dbNameField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(this, "Name must not be empty",
                    "Incorrect input", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    private boolean checkDBLoc() {
        File f = new File(dbLocField.getText());
        if (!(f.exists() && f.isDirectory())) {
            JOptionPane.showMessageDialog(this, "Invalid database directory",
                    "Incorrect input", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    private boolean checkOPMFile() {
        File f = new File(opmFileField.getText());
        if (!f.exists() || !f.isFile()) {
            JOptionPane.showMessageDialog(this, "Invalid OPM file",
                    "Incorrect input", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        return true;
    }

    private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmButtonActionPerformed
        if (!(checkDBName() && checkDBLoc() && checkOPMFile())) {
            return;
        }
        confirmed = true;
        dispose();
    }//GEN-LAST:event_confirmButtonActionPerformed

    private void browseButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButton1ActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setMultiSelectionEnabled(false);
        jfc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        jfc.setDialogTitle("Choose database location");
        jfc.showDialog(this, "Choose");
        File f = jfc.getSelectedFile();
        if (f != null) {
            dbLocField.setText(f.getAbsolutePath());
        }
        repaint();
    }//GEN-LAST:event_browseButton1ActionPerformed

    private void browseButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_browseButton2ActionPerformed
        JFileChooser jfc = new JFileChooser();
        jfc.setFileFilter(new javax.swing.filechooser.FileFilter() {

            @Override
            public boolean accept(File f) {
                return f.getName().endsWith(".rdf") || f.isDirectory();
            }

            @Override
            public String getDescription() {
                return "*.rdf";
            }
        });
        jfc.setDialogTitle("Choose OPM file location");
        jfc.setMultiSelectionEnabled(false);
        jfc.showDialog(this, "Choose");
        File f = jfc.getSelectedFile();
        if (f != null) {
            opmFileField.setText(f.getAbsolutePath());
        }
    }//GEN-LAST:event_browseButton2ActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void valuesSourceComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_valuesSourceComboBoxActionPerformed
        loadValuesForm();
    }//GEN-LAST:event_valuesSourceComboBoxActionPerformed

    private void loadValuesForm() {
        switch (valuesSourceComboBox.getSelectedIndex()) {
            case 0:
                CardLayout cl = (CardLayout) valuesPanel.getLayout();
                cl.show(valuesPanel, "Janus");
                valuesPanel.repaint();
                currPanel = janusPanel;
                break;
            default:
                break;
        }
    }

    private void importValuesCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_importValuesCheckBoxActionPerformed
        if (importValuesCheckBox.isSelected()) {
            valuesSourceComboBox.setEnabled(true);
            loadValuesForm();
            currPanel.setActive(true);
        } else {
            currPanel.setActive(false);
            valuesSourceComboBox.setEnabled(false);
        }
    }//GEN-LAST:event_importValuesCheckBoxActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton browseButton1;
    private javax.swing.JButton browseButton2;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel comboBoxLabel;
    private javax.swing.JButton confirmButton;
    private javax.swing.JTextField dbLocField;
    private javax.swing.JLabel dbLocLabel;
    private javax.swing.JTextField dbNameField;
    private javax.swing.JLabel dbNameLabel;
    private javax.swing.JCheckBox importValuesCheckBox;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextField opmFileField;
    private javax.swing.JLabel opmFileLabel;
    private javax.swing.JPanel valuesPanel;
    private javax.swing.JComboBox valuesSourceComboBox;
    // End of variables declaration//GEN-END:variables
}
